package com.zdkj.subway.check.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.subway.check.mapper.SysUserMapper;
import com.zdkj.subway.check.util.UserInfo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class Page<E> {
    Integer pageNo;
    Integer pageSize;
    List<E> data;
    Integer total;
    Integer totalPage;
    Integer currentPage;

    /**
     *
     * @param data 展示的数据
     * @param addPermission 是否加车站权限
     */
    public void setData(List<E> data,boolean... addPermission) {
        if(addPermission.length>0 && addPermission[0]){
            data=addPermission(data);
        }
        if(pageNo==null && currentPage==null){
            pageNo=1;
            currentPage=1;
        }
        if(pageSize==null){
            pageSize=1000000;
        }
        total=data.size();
        totalPage=data.size()/pageSize+(data.size()%pageSize==0?0:1);
        pageNo=pageNo==null?currentPage:pageNo;
        int index=(pageNo-1)*pageSize;
        if(index>=data.size()){
            index=0;
            pageNo=1;
            currentPage=1;
        }

        int end=(pageNo)*pageSize;
        end=end>=data.size()?data.size():end;
        List<E> objects=new ArrayList<>();
        if(end>=0){
            objects = data.subList(index, end);
        }
        this.data = objects;
    }

    /**
     * 包含线路，站点过滤
     * @param data
     */
    public List<E> addPermission(List<E> data) {
        SysUserMapper sysUserMapper = SpringContextHolder.getBean(SysUserMapper.class);
        Map<String,Object> param=new HashMap<>();
        param.put("user_code", UserInfo.getUserCode());
        List<SysUser> sysUsers = sysUserMapper.selectByMap(param);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        //拥有的线路，站点
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);

        //获取每个线路对应的所有站
        Map<String,List<String>> linesAndStations=new HashMap<>();

        ////////////////////////////////////////////////////////
        if(sysUser.getBelongToLine()){
            if(StringUtils.isNotEmpty(managementLineStation)){
                //获取每个线路对应的所有站
                for(int i=0;i<managementLineStations.size();i++){
                    JSONObject jsonObject = managementLineStations.getJSONObject(i);
                    String line = jsonObject.getString("line");
                    String station = jsonObject.getString("station");
                    if(linesAndStations.containsKey(line) && StringUtils.isNotEmpty(station)){
                        List<String> l = linesAndStations.get(line);
                        l.add(station);
                    }else if(!linesAndStations.containsKey(line)){
                        List<String> l=new ArrayList<>();
                        if(StringUtils.isNotEmpty(station)){
                            l.add(station);
                        }
                        linesAndStations.put(line,l);
                    }
                }
            }
        }else {
            if("[]".equals(managementLineStation)){
                List<String> l=new ArrayList<>();
                linesAndStations.put(sysUser.getLineCode(),l);
            }else {
                //获取每个线路对应的所有站
                for(int i=0;i<managementLineStations.size();i++){
                    JSONObject jsonObject = managementLineStations.getJSONObject(i);
                    String line = jsonObject.getString("line");
                    String station = jsonObject.getString("station");
                    if(linesAndStations.containsKey(line) && StringUtils.isNotEmpty(station)){
                        List<String> l = linesAndStations.get(line);
                        l.add(station);
                    }else if(!linesAndStations.containsKey(line)){
                        List<String> l=new ArrayList<>();
                        if(StringUtils.isNotEmpty(station)){
                            l.add(station);
                        }
                        linesAndStations.put(line,l);
                    }
                }
            }
        }




        /////////////////////////////////////////////////////
        List<E> res=new ArrayList<>();
        for(E o:data){
            String s = JSONObject.toJSONString(o);
            JSONObject oneData =JSONObject.parseObject(s);
            String line = oneData.getString("lineCode");
            if(StringUtils.isEmpty(line)){
                line =oneData.getString("submitLineCode");
            }
            String stationCode =oneData.getString("stationCode");
            if(StringUtils.isEmpty(stationCode)){
                stationCode =oneData.getString("submitStationCode");
            }
            //有线路，但是无车站，说明这个线路的都可以
            if(linesAndStations.containsKey(line) && linesAndStations.get(line).size()==0){
                res.add(o);
            }
            //有线路，有车站，说明这个线路和车站的可以
            else if(linesAndStations.containsKey(line) && linesAndStations.get(line).size()>0
                    && linesAndStations.get(line).contains(stationCode)){
                res.add(o);
            }
        }
        return res;
    }

}
